<?php

namespace Xrfg\Api\Trading\Account\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Xrfg\Core\Trading\Account\Models\Coin;

class AddAddressRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'coin_id' => ['required', 'exists:trading_coins,id'],
            'address' => [
                'required',
                'walletAddressValidator:'.$this->input('coin_id').','.$this->input('chain_name'),
                Rule::unique('trading_addresses')
                    ->where('user_id', $this->user()->id)
                    ->where('coin_id', $this->input('coin_id')),
            ],
            'note' => ['required'],
            'tag' => ['nullable'],
        ];

        $coin = Coin::find($this->input('coin_id'));
        if ($coin && 'USDT' == $coin->name) {
            $rules['chain_name'] = ['required', Rule::in(['OMNI', 'ERC20'])];
        }

        return $rules;
    }
}
